package ar.unc.famaf.pln.giveselector.context;

import java.io.Serializable;

/**
 * An Utterance is a text associated to a context.
 * @author Alexandre Denis
 */
@SuppressWarnings("serial")
public class Utterance implements Serializable
{
	private static int icounter = 0;	// Instance counter
	private final int id; 				// Identifier for this utt
	private final String text;
	private final Context context;
	private final Response response; 	// the actions that are executed by the DF 
										// as a response to the DG utterance

    /**
	 * Creates a new Utterance with given text and response.
	 * @param text
	 * @param response
	 */
	public Utterance(String text, Response response)
	{
		this.id = ++Utterance.icounter;
		this.text = text;
		this.context = null;
		this.response = response;
	}
    
	/**
	 * Creates a new Utterance with given text, context and response.
	 * @param text
	 * @param features
	 * @param response
	 */
	public Utterance(String text, Context features, Response response)
	{
		this.id = ++Utterance.icounter;
		this.text = text;
		this.context = features;
		this.response = response;
	}
	
	/**
	 * Creates a new Utterance with given id, text, context and response.
	 * @param text
	 * @param features
	 * @param response
	 */
	public Utterance(int id, String text, Context features, Response response)
	{
		this.id = id;
		this.text = text;
		this.context = features;
		this.response = response;
	}

	
	/**
	 * @return the id
	 */
	public int getId() {
		return id;
	}

	
	/**
	 * @return the text
	 */
	public String getText()
	{
		return text;
	}
	
	
	/**
	 * @return the context
	 */
	public Context getContext()
	{
		return context;
	}
	
	/**
	 * @return the response
	 */
	public Response getResponse()
	{
		return response;
	}


	/**
	 * @return a String representation of this Utterance.
	 */
	public String toString()
	{
		return "[" + id + "] :" + text;
	}


	@Override
	public int hashCode()
	{
		final int prime = 31;
		int result = 1;
		result = prime * result + ((context == null) ? 0 : context.hashCode());
		result = prime * result + ((response == null) ? 0 : response.hashCode());
		result = prime * result + ((text == null) ? 0 : text.hashCode());
		return result;
	}

	/**
	 * Compares two Utterance objects. Two utt are equals if its text, context 
	 * and response are equal. The id attr is not used to determine this, in this way
	 * it is easier to create a set of utt with unique objects.
	 */
	@Override
	public boolean equals(Object obj)
	{
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Utterance other = (Utterance) obj;
		if (context == null)
		{
			if (other.context != null)
				return false;
		}
		else if (!context.equals(other.context))
			return false;
		if (response == null)
		{
			if (other.response != null)
				return false;
		}
		else if (!response.equals(other.response))
			return false;
		if (text == null)
		{
			if (other.text != null)
				return false;
		}
		else if (!text.equals(other.text))
			return false;
		return true;
	}

}
